﻿Imports System.Globalization
Imports System.Threading

''' <summary>
''' Secssion của ứng dụng
''' </summary>
''' <remarks></remarks>
Public Class ApplicationSession
    'Private _dtOption As DataSetSystem.AppOptionDataTable = Nothing
    Private Shared _dsPermission As DataSetPermission = Nothing

    Public Sub New()
    End Sub

    Public Sub New(ByVal dtOption As DataSetSystem.AppOptionDataTable)
        _dsPermission = New DataSetPermission
        BuildPermissionInfo.BuildActionTable(_dsPermission.ActionList)
        'BuildPermissionInfo.BuildPermissionGroupTable(_dsPermission.PermissonGroup)
        BuildPermissionInfo.BuildPermissionTable(_dsPermission.PermissionList)
        BuildPermissionInfo.BuildPermissionMappingTable(_dsPermission)

        _dtOption = dtOption
        'Me.LoadNumberFormat()
    End Sub


    Private _isLogIn As Boolean

    Public Property IsLogIn() As String
        Get
            Return _isLogIn
        End Get
        Set(ByVal value As String)
            _isLogIn = value
        End Set
    End Property


#Region "Current LogIn"

    Public ReadOnly Property PermissionDataObject() As DataSetPermission
        Get
            Return _dsPermission
        End Get
    End Property

    Private _currentUser As String

    Public Property CurrentUser() As String
        Get
            Return _currentUser
        End Get
        Set(ByVal value As String)
            _currentUser = value
        End Set
    End Property

#End Region

#Region "Option"

    Private _dtOption As DataSetSystem.AppOptionDataTable

    Public Property OptionDataObject() As DataSetSystem.AppOptionDataTable
        Get
            Return _dtOption
        End Get
        Set(ByVal value As DataSetSystem.AppOptionDataTable)
            _dtOption = value
        End Set
    End Property

#Region "Constant"

    'NumberFormat
    Public Const mscCurrencyDecimalDigits As String = "CurrencyDecimalDigits"

    Public Const mscCurrencySymbol As String = "CurrencySymbol"

    Public Const mscPositivePattern As String = "PositivePattern"

    Public Const mscNegativePattern As String = "NegativePattern"

    Public Const mscNumberDecimalDigits As String = "NumberDecimalDigits"

    Public Const mscPercentDecimalDigits As String = "PercentDecimalDigits"

    Public Const mscGroupSeparator As String = "GroupSeparator"

    Public Const mscDecimalSeparator As String = "DecimalSeparator"

    'CompanyInfo
    Public Const mscCompanyName As String = "CompanyName"

    Public Const mscCompanyAddress As String = "CompanyAddress"

    Public Const mscCompanyPhone As String = "CompanyPhone"

    Public Const mscCompanyWebSite As String = "CompanyWebSite"

    'Sign Info
    Public Const mscDirectorName As String = "DirectorName"

    Public Const mscCashKeeper As String = "CashKeeper"

    Public Const mscStockKeeper As String = "StockKeeper"

    Public Const mscChiefAccountor As String = "ChiefAccountor"

    Public Const mscCreatedReportor As String = "CreatedReportor"

    Public Const mscIsPrintDateSignInReport As String = "IsPrintDateSignInReport"

    Public Const mscIsPrintSignInReport As String = "IsPrintSignInReport"

    'Database Info
    Public Const mscAutoBackUpWhenClose As String = "AutoBackUpWhenClose"

    Public Const mscBackUpDirectory As String = "BackUpDirectory"

    'Common
    Public Const mscOptionID As String = "OptionID"

    Public Const mscOptionType As String = "OptionType"

    Public Const mscOptionValue As String = "OptionValue"

    Public Const mscDefaultValue As String = "DefaultValue"

    Public Const mscIsSystem As String = "IsSystem"

#End Region

#Region "Number Format"

    Private _numberFormatInfo As NumberFormatInfo

    Public Property NumberFormat() As NumberFormatInfo
        Get
            If _numberFormatInfo Is Nothing Then
                _numberFormatInfo = Thread.CurrentThread.CurrentCulture.NumberFormat
            End If
            Return _numberFormatInfo
        End Get
        Set(ByVal value As NumberFormatInfo)
            _numberFormatInfo = value
            Me.SetSystemNumberFormat(_numberFormatInfo)
        End Set
    End Property

    'Private m_iCurrencyDecimalDigits As Integer
    'Số chữ số phần thập phân của số tiền
    'Private m_iCurrencyDecimalDigitsInReport As Integer
    'Số chữ số phần thập phân của số tiền (Trên sổ sách chứng từ )
    'Private m_sCurrencySymbol As String = ""
    'Biểu tượng của số tiền. VD: đ, $
    'Private m_sCurrencySymbolInReport As String = ""
    'Biểu tượng của số tiền. VD: đ, $
    'Private m_iCurrencyPositivePattern As Integer = 1
    'Cách viết số dương của số tiền
    'Private m_iCurrencyPositivePatternInReport As Integer = 1
    'Cách viết số dương của số tiền (Trên sổ sách chứng từ )

    'Private m_iCurrencyNegativePattern As Integer
    'Cách viết số âm của số tiền


    Public Function SetSystemNumberFormat(ByVal nfi As NumberFormatInfo) As Boolean
        Try
            Thread.CurrentThread.CurrentCulture.NumberFormat = nfi
            Return True
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Sub LoadNumberFormat()
        Dim oDic As New Dictionary(Of String, Object)
        If _dtOption IsNot Nothing AndAlso _dtOption.Count > 0 Then
            For Each dr As DataSetSystem.AppOptionRow In _dtOption.Rows
                Select Case dr.OptionID
                    Case mscCurrencyDecimalDigits
                        oDic.Add(dr.OptionID, CInt(dr.OptionValue))
                    Case mscNegativePattern
                        oDic.Add(dr.OptionID, dr.OptionValue)
                    Case mscPositivePattern
                        oDic.Add(dr.OptionID, dr.OptionValue)
                    Case mscCurrencySymbol
                        oDic.Add(dr.OptionID, dr.OptionValue)
                    Case mscNumberDecimalDigits
                        oDic.Add(dr.OptionID, CInt(dr.OptionValue))
                    Case mscPercentDecimalDigits
                        oDic.Add(dr.OptionID, CInt(dr.OptionValue))
                    Case mscGroupSeparator
                        oDic.Add(dr.OptionID, dr.OptionValue)
                    Case mscDecimalSeparator
                        oDic.Add(dr.OptionID, dr.OptionValue)
                End Select
            Next


        End If

        If Not oDic.ContainsKey(mscCurrencyDecimalDigits) Then
            oDic.Add(mscCurrencyDecimalDigits, 2)
        End If

        If Not oDic.ContainsKey(mscNegativePattern) Then
            'oDic.Add(mscCurrencyDecimalDigits, 2)
        End If

        If Not oDic.ContainsKey(mscPositivePattern) Then

        End If

        If Not oDic.ContainsKey(mscCurrencySymbol) Then
            oDic.Add(mscCurrencySymbol, "")
        End If

        If Not oDic.ContainsKey(mscNumberDecimalDigits) Then
            oDic.Add(mscNumberDecimalDigits, 2)
        End If

        If Not oDic.ContainsKey(mscPercentDecimalDigits) Then
            oDic.Add(mscPercentDecimalDigits, 2)
        End If

        If Not oDic.ContainsKey(mscGroupSeparator) Then
            oDic.Add(mscGroupSeparator, ".")
        End If

        If Not oDic.ContainsKey(mscDecimalSeparator) Then
            oDic.Add(mscDecimalSeparator, ",")
        End If

        Me.SetNumberFormatInfo(oDic)
    End Sub

    Public Function SetNumberFormatInfo(ByVal oDictionary As Dictionary(Of String, Object)) As Boolean
        If oDictionary IsNot Nothing AndAlso oDictionary.Count > 0 Then
            Dim oNfi As New NumberFormatInfo()
            With oNfi
                For Each skey As String In oDictionary.Keys
                    Select Case skey
                        Case mscCurrencyDecimalDigits
                            .CurrencyDecimalDigits = oDictionary.Item(skey)
                        Case mscNegativePattern
                            .CurrencyNegativePattern = oDictionary.Item(skey)
                            .NumberNegativePattern = oDictionary.Item(skey)
                            .PercentNegativePattern = oDictionary.Item(skey)
                        Case mscPositivePattern
                            .CurrencyPositivePattern = oDictionary.Item(skey)
                            .PercentPositivePattern = oDictionary.Item(skey)
                        Case mscCurrencySymbol
                            .CurrencySymbol = oDictionary.Item(skey)
                        Case mscNumberDecimalDigits
                            .NumberDecimalDigits = oDictionary.Item(skey)
                        Case mscPercentDecimalDigits
                            .PercentDecimalDigits = oDictionary.Item(skey)
                        Case mscGroupSeparator
                            .CurrencyGroupSeparator = oDictionary.Item(skey)
                            .NumberGroupSeparator = oDictionary.Item(skey)
                            .PercentGroupSeparator = oDictionary.Item(skey)
                        Case mscDecimalSeparator
                            .CurrencyDecimalSeparator = oDictionary.Item(skey)
                            .NumberDecimalSeparator = oDictionary.Item(skey)
                            .PercentDecimalSeparator = oDictionary.Item(skey)
                    End Select
                Next
            End With
            Me.NumberFormat = oNfi
            Return True
        End If
        Return False
    End Function

    Public Sub SetNumberFormatForSave()
        Me.SetSessionPropertiesForSave(mscCurrencyDecimalDigits, Me.NumberFormat.CurrencyDecimalDigits, , _
                                       EnumDataType.TypeNumber)
        Me.SetSessionPropertiesForSave(mscNegativePattern, Me.NumberFormat.CurrencyNegativePattern, , _
                                       EnumDataType.TypeString)
        Me.SetSessionPropertiesForSave(mscPositivePattern, Me.NumberFormat.CurrencyPositivePattern, , _
                                       EnumDataType.TypeString)
        Me.SetSessionPropertiesForSave(mscCurrencySymbol, Me.NumberFormat.CurrencySymbol, , EnumDataType.TypeString)
        Me.SetSessionPropertiesForSave(mscNumberDecimalDigits, Me.NumberFormat.NumberDecimalDigits, , _
                                       EnumDataType.TypeNumber)
        Me.SetSessionPropertiesForSave(mscPercentDecimalDigits, Me.NumberFormat.PercentDecimalDigits, , _
                                       EnumDataType.TypeNumber)
        Me.SetSessionPropertiesForSave(mscGroupSeparator, Me.NumberFormat.CurrencyGroupSeparator, , _
                                       EnumDataType.TypeString)
        Me.SetSessionPropertiesForSave(mscDecimalSeparator, Me.NumberFormat.CurrencyDecimalSeparator, , _
                                       EnumDataType.TypeString)
    End Sub

#End Region

#Region "CompanyInfo"


    Private _companyName As String

    Public Property CompanyName() As String
        Get
            Return _companyName
        End Get
        Set(ByVal value As String)
            _companyName = value
        End Set
    End Property


    Private _companyAddress As String

    Public Property CompanyAddress() As String
        Get
            Return _companyAddress
        End Get
        Set(ByVal value As String)
            _companyAddress = value
        End Set
    End Property


    Private _companyPhone As String

    Public Property CompanyPhone() As String
        Get
            Return _companyPhone
        End Get
        Set(ByVal value As String)
            _companyPhone = value
        End Set
    End Property


    Private _licenseInfo As String

    Public Property LicenseInfo() As String
        Get
            Return _licenseInfo
        End Get
        Set(ByVal value As String)
            _licenseInfo = value
        End Set
    End Property


    Private _companyWebSite As String

    Public Property CompanyWebSite() As String
        Get
            Return _companyWebSite
        End Get
        Set(ByVal value As String)
            _companyWebSite = value
        End Set
    End Property

#End Region

#Region "SignInfo"


    Private _directorName As String

    Public Property DirectorName() As String
        Get
            Return _directorName
        End Get
        Set(ByVal value As String)
            _directorName = value
        End Set
    End Property


    Private _chiefAccountor As String

    Public Property ChiefAccountor() As String
        Get
            Return _chiefAccountor
        End Get
        Set(ByVal value As String)
            _chiefAccountor = value
        End Set
    End Property


    Private _cashKeeper As String

    Public Property CashKeeper() As String
        Get
            Return _cashKeeper
        End Get
        Set(ByVal value As String)
            _cashKeeper = value
        End Set
    End Property


    Private _stockKeeper As String

    Public Property StockKeeper() As String
        Get
            Return _stockKeeper
        End Get
        Set(ByVal value As String)
            _stockKeeper = value
        End Set
    End Property


    Private _createReportor As String

    Public Property CreatedReportor() As String
        Get
            Return _createReportor
        End Get
        Set(ByVal value As String)
            _createReportor = value
        End Set
    End Property


    Private _printSignInReport As Boolean

    Public Property IsPrintSignInReport() As Boolean
        Get
            Return _printSignInReport
        End Get
        Set(ByVal value As Boolean)
            _printSignInReport = value
        End Set
    End Property


    Private _printDateSignInReport As Boolean

    Public Property IsPrintDateSignInReport() As Boolean
        Get
            Return _printDateSignInReport
        End Get
        Set(ByVal value As Boolean)
            _printDateSignInReport = value
        End Set
    End Property


#End Region

#Region "DatabaseInfo"

    Private _currentDatabaseName As String

    Public Property CurrentDatabaseName() As String
        Get
            Return _currentDatabaseName
        End Get
        Set(ByVal value As String)
            _currentDatabaseName = value
        End Set
    End Property


    Private _currentServerName As String

    Public Property CurrentServerName() As String
        Get
            Return _currentServerName
        End Get
        Set(ByVal value As String)
            _currentServerName = value
        End Set
    End Property


    Private _currentSQLInstanceName As String

    Public Property CurrentSQLInstanceName() As String
        Get
            Return _currentSQLInstanceName
        End Get
        Set(ByVal value As String)
            _currentSQLInstanceName = value
        End Set
    End Property


    Private _createdDateDatabase As Date

    Public Property CreatedDateDatabase() As Date
        Get
            Return _createdDateDatabase
        End Get
        Set(ByVal value As Date)
            _createdDateDatabase = value
        End Set
    End Property


    Private _autoBackUpWhenClose As Boolean

    Public Property AutoBackUpWhenClose() As Boolean
        Get
            Return _autoBackUpWhenClose
        End Get
        Set(ByVal value As Boolean)
            _autoBackUpWhenClose = value
        End Set
    End Property


    Private _backUpDirectory As String

    Public Property BackUpDirectory() As String
        Get
            Return _backUpDirectory
        End Get
        Set(ByVal value As String)
            _backUpDirectory = value
        End Set
    End Property

#End Region


#Region "Common Function"

    Public Sub SetSessionProperties(ByRef sessionProperty As Object, ByVal optionID As String, _
                                    Optional ByVal optionDefaultValue As Object = Nothing, _
                                    Optional ByVal enumValueType As EnumDataType = EnumDataType.TypeString, _
                                    Optional ByVal IsSystem As Boolean = False, _
                                    Optional ByVal Description As String = "")
        Try
            Dim drOption As DataSetSystem.AppOptionRow = _dtOption.FindByOptionID(optionID)
            If drOption IsNot Nothing Then
                If _
                    drOption.IsOptionValueNull OrElse _
                    String.IsNullOrEmpty(drOption.OptionValue) AndAlso enumValueType <> EnumDataType.TypeString Then
                    If IsNothing(optionDefaultValue) Then
                        Select Case enumValueType '0: String; 1: Number; 2: Date; 3: Boolean; 4: GUID
                            Case EnumDataType.TypeNumber
                                drOption.OptionValue = 0
                            Case EnumDataType.TypeDate
                                drOption.OptionValue = Date.Today
                            Case EnumDataType.TypeBoolean
                                drOption.OptionValue = False
                            Case EnumDataType.TypeGuid
                                drOption.OptionValue = Guid.Empty.ToString
                        End Select

                    Else
                        drOption(mscOptionValue) = optionDefaultValue
                    End If
                ElseIf _
                    drOption(mscOptionValue).ToString.Trim = "" AndAlso enumValueType = EnumDataType.TypeString AndAlso _
                    (optionID = mscCompanyName OrElse optionID = mscCompanyAddress) Then
                    drOption(mscOptionValue) = optionDefaultValue
                End If
                If enumValueType = EnumDataType.TypeGuid Then
                    sessionProperty = New Guid(drOption(mscOptionValue).ToString)
                Else
                    sessionProperty = drOption(mscOptionValue)
                End If
            Else
                Dim drRow As DataSetSystem.AppOptionRow = Nothing
                drRow = _dtOption.NewRow
                drRow(mscOptionID) = optionID
                drRow(mscOptionValue) = optionDefaultValue
                drRow(mscOptionType) = enumValueType
                drRow(mscIsSystem) = IsSystem
                drRow("Description") = Description
                _dtOption.Rows.Add(drRow)
                'SaveAllOption()
                sessionProperty = optionDefaultValue
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub

    Public Sub SetSessionPropertiesForSave(ByVal optionID As String, ByVal optionValue As Object, _
                                           Optional ByVal optionDefaultValue As Object = Nothing, _
                                           Optional ByVal enumValueType As EnumDataType = EnumDataType.TypeString, _
                                           Optional ByVal IsSystem As Boolean = False, _
                                           Optional ByVal Description As String = "")

        Try
            Dim drOption As DataSetSystem.AppOptionRow = _dtOption.FindByOptionID(optionID)
            If drOption IsNot Nothing Then
                drOption(mscOptionValue) = optionValue
            Else
                Dim drRow As DataSetSystem.AppOptionRow = Nothing
                drRow = _dtOption.NewRow
                drRow(mscOptionID) = optionID
                drRow(mscOptionValue) = optionValue
                drRow(mscOptionType) = enumValueType
                drRow(mscIsSystem) = IsSystem
                drRow("Description") = Description
                _dtOption.Rows.Add(drRow)
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub

#End Region

#End Region
End Class
